package chatManagers;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

/**
 *
 * @author spring
 * @version
 */
public class chat extends HttpServlet {
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //DBMSManager dbm= new DBMSManager(request.getParameter("db"));
        DBMSManager dbm = new DBMSManager();
        
        response.setContentType("text/plain");
        response.setHeader("Cache-control", "no-cache");
        PrintWriter out = response.getWriter();
        
        String responseString = new String();
        String comment = "";
        String uName = "";
        
        String method = request.getParameter("doAction");
        int uid = new Integer(request.getParameter("userid")).intValue();
        int cid = new Integer(request.getParameter("chatid")).intValue();
        int lmid = 0;
        if(!request.getParameter("lastmsgid").equals("")) {
            lmid = new Integer(request.getParameter("lastmsgid")).intValue();
        }
                
        // If the user is signing on or off, prepare a comment for the db
        if (method.equals("signon")) {
            comment = "<span style=\"color: #009900;\"><i> has joined the chat.</i></span>";
        } else if (method.equals("signoff")){
            comment = "<span style=\"color: #990000;\"><i> has left the chat.</i></span>";
        } else if (method.equals("addcomment")){
            comment = request.getParameter("comment");
        }
        
        // Be sure this is an active chat
        if (cid > 0) {
            dbm.AddPost(cid, uid, comment);
        }
        
        //System.out.println("Loading Posts in chat");
        ResultSet rs = dbm.getLatestPosts(cid,lmid);
        if (rs==null) {
            out.print("<p>NO POSTS TO CHAT</p>");
        } else {
            try{
                while(rs.next()) {
                    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("h:mm aa");
                    String user = rs.getString("uName");
                    //user = user.substring(0, (user.length() - 2));               
                    java.sql.Time d = rs.getTime("mtime");
                    String message = rs.getString("message");
                    String mID = rs.getString("mid");
                    
                    String dt = df.format(d);
                    responseString += "<p id=\"" + mID + "\">"
                            + "<b>" + user +"</b>: "
                            + "<span class=\"datetime\">(" + dt + ")</span> "
                            + message + "</p>";
                }
                rs.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } finally {
                dbm.closeConnection();
            }
        }
        out.print(responseString);
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
